package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjitc.pigspring.bean.po.*;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.GoodsService;
import com.zjitc.pigspring.service.ShopService;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/shop")
@Slf4j
public class ShopController {

    @Autowired
    private ShopService shopService;

    @GetMapping("/getAll")
    @ResponseBody
    public Result<List<ShopVO>> getShop(){
        List<ShopVO> list = shopService.getAll();
        if (list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @GetMapping("/getByUser/{id}")
    @ResponseBody
    public Result<List<ShopVO>> getShopByUser(@PathVariable Integer id){
        List<ShopVO> list = shopService.getAllByUser(id);
        if (list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @DeleteMapping("/deleteOne/{id}")
    public Result deleteUser(@PathVariable Integer id){
        boolean flag = shopService.removeById(id);
        if (flag){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PostMapping("/addOne")
    public Result addShop(@RequestBody Shop shop){
        QueryWrapper<Shop> wrapper = new QueryWrapper<Shop>()
                .eq("user_id",shop.getUserId())
                .eq("good_id",shop.getGoodId());
        Shop one = shopService.getOne(wrapper);
        if (one!=null){
            return Result.error(Codes.SHOP_GOOD_EXISTS);
        }
        boolean save = shopService.save(shop);
        if (save){
            return Result.success(Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @PutMapping("/updateOne")
    public Result updateShop(@RequestBody Shop shop){

        QueryWrapper<Shop> wrapper = new QueryWrapper<Shop>()
                .eq("user_id",shop.getUserId())
                .eq("good_id",shop.getGoodId());
        Shop one = shopService.getOne(wrapper);
        if(one!=null){
            return Result.error(Codes.GOOD_EXISTS);
        }
        boolean flag = shopService.updateById(shop);
        if(flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<Shop> selectOne(@PathVariable Integer id){
        Shop byId = shopService.getById(id);
        if (byId!=null){
            return Result.success(byId);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<ShopVO>> queryShop(@RequestBody ShopVO shopVO){

        List<ShopVO> list = shopService.getListByUsername(shopVO);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }
}

